﻿<?php
	session_start();
	require_once('php/include/global.php');
	$errors = array();
	
	$profile = new Profile();
	$user = new User();
	
	if(isset($_SESSION['user'])) {
		$user = unserialize($_SESSION['user']);
	}
	
	/**************** AZIONE SU LOGIN ****************/
	
	if(isset($_POST['login'])) {
		$email = $_POST['email'];
		$password = md5($_POST['password']); 
	
		if(!$user->login($email, $password)) {
			$errors['login'] = true;
		}
	}
	
	/**************** INIZIO REGISTRAZIONE ****************/
	
	if(!$user->isLogged()) {
		if (isset($_REQUEST['reg_submit'])) {
			if($user->validateField("name", $_POST['name'])) {
				$user->name = $_POST['name'];
			} else {
				$errors['name'] = "Nome non valido!";
			}
			
			if($user->validateField("surname", $_POST['surname'])) {
				$user->surname = $_POST['surname'];
			} else {
				$errors['surname'] = "Cognome non valido!";
			}
			if($user->validateField("email", $_POST['reg_email'])) {
				if($user->validateField("emailFree", $_POST['reg_email'])) {
					$user->email = $_POST['reg_email'];
				} else {
					$errors['email'] = "L'indirizzo email indicato &egrave; gi&agrave; registrato";
				}
			} else {
				$errors['email'] = "Email non valida!";
			}
			
			if($_POST['password'] == "") {
				$errors['password'] = "Inserisci la password desiderata!";
			} else {
				if($_POST['rpassword'] != $_POST['password'] OR $_POST['rpassword'] == "") {
					$errors['password'] = "Le due password non corrispondono!";
				} else {
					$user->password = md5($_POST['password']);
				}
			}
			if(!isset($_POST['rule'])){
				$errors['rule'] = "Il regolamento non &egrave; stato accettato!";
			}
			$user->level = 0;
		}
		if(isset($_REQUEST['reg_submit']) && count($errors)==0) {
			$resultUser = $user->toDB();
			
			
			if(!$resultUser) {
				echo "<script>alert('Errore nell\'inserimento dell\'utente')</script>";
			}else{
				
				$profile->user_id = $resultUser;
				$resultProfile = $profile->toDB();
				if(!$resultProfile){
					echo "<script>alert('Errore nell\'inserimento del profilo)</script>";
				}else {					
					$message = "Ciao " . $user->name . ' ' . $user->surname . "\n
					per attivare l'account da lei registrato su blackjack.steh.ch la invitiamo a visitare il seguente collegamento\n
					http://blackjack.steh.ch/index.php?p=activate&email=" . $user->email . "&activation_id=ddads4234dfs2\n
					\n\n
					Se dovesse avere ricevuto questa email per sbaglio la invitiamo a cancellarla.\n
					Lo staff di steh.ch.";
					
					//mail($user->email, "BlackJack - Conferma iscrizione", $message);
					
					$feedback_login = $user->login($user->email, $user->password);
				}
				
				
			}
		}
	}
	
	/**************** FINE REGISTRAZIONE ****************/
	
	if(isset($_POST['logout'])) {
		$user->logout();
	}
	
	ob_start();
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Blackjack</title>
        <?php
			if(!$user->isLogged()){
				 echo '<link rel="stylesheet" type="text/css" href="static/css/home.css">';
			}else{
				 echo '<link rel="stylesheet" type="text/css" href="static/css/default.css">';
			}
		?>
       
        <script language="JavaScript" src="static/js/jquery-1.3.2.min.js"></script>
 		<script language="JavaScript" src="static/js/ajax.js"></script>
		<script language="JavaScript" src="static/js/library.js"></script>
		<script language="JavaScript" src="static/js/ajax_framework.js"></script>

		<script language="JavaScript">
			function popUp(URL) {
				day = new Date();
				id = day.getTime();
				eval("page" + id + " = window.open(URL, '" + id + "', 'toolbars=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=600');");
			}
		</script>
		
    </head>
    <body>
        <div id="container">
        	<?php 
        		require('php/pages/header.php');
        		require('php/pages/center.php');
        		require('php/pages/footer.php');
        	?>
		</div>
    </body>
<?php
	ob_flush();
?>
</html>